home *** CD-ROM | disk | FTP | other *** search
- /*****************************************************************************
- SoundsFn.js
-
- Encarta Virtual Globe 1999
-
- (c) Copyright Microsoft Corporation 1998
-
- Fly Through has site specific sounds/music. This include file contains the
- functions which return which
- index to use in the media source nodes of the media graph. It is assumed
- that the data tables here are in sync with the MediaGraph control nodes,
- that contain the paths to the proper music. If one changes, make sure the
- other does as well. The media graph can be altered using AudioMan's
- MediaAuthor and the Sounds.mgd file. Cut and paste the control specification
- in to the TerrainCtl.htm file.
-
- This file is to be included by Fly*.htm
-
- Owner = WHsu
- *****************************************************************************/
-
- function LoadSounds()
- {
- g_WindSoundNode = MediaGraph.Nodes("WindSound");
- g_RadioSoundsNode = MediaGraph.Nodes("RadioSounds");
-
- LoadFusionMedia();
- MediaGraph.Nodes("WindSound").LoadMedia(false);
- }
-
- function PlaySound()
- {
- // Decide which sound to play. Make sure this is only called when safe (OnLoad and OnStop)
- PlayIndex = g_PlayIndex; // inc before play in case Play* calls PlaySound again
- g_PlayIndex = (g_PlayIndex+1) % g_PlayList.length;
- eval( g_PlayList[PlayIndex] );
- // G_DEBUG_PLAY_COUNT++;
- // window.status = "Play Index = " + g_PlayIndex + " count = " + G_DEBUG_PLAY_COUNT; // TEMP, FOR DEBUGGING
- }
-
- function PlayWind()
- {
- if ( MediaGraph.Nodes("WindSound").IsAvailable )
- {
- MediaGraph.Nodes("WindChannel").Play();
- } else {
- PlaySound();
- MediaGraph.Nodes("WindSound").LoadMedia(false);
- }
- }
-
- function PlayFusion()
- {
- // Might do some random selection of which fusion source we want to use later
- if ( g_fFusionSrcAllLoaded )
- {
- PlayFusionNow();
- } else {
- PlaySound();
- LoadFusionMedia();
- }
- }
-
- function PlayFusionNow()
- {
- MediaGraph.Nodes("FusionChannel").Play()
- }
-
- function PlaySiteSound()
- {
-
- // find the nearest site sound
- var Lat, Long, index;
- Lat = TerrainCtl.PositionLat;
- Long = TerrainCtl.PositionLong;
- index = FindNearestSound( Lat, Long, g_Range[g_Continent] );
- if ( index != 0 )
- {
- MediaGraph.Nodes("RadioSounds").LoadMedia(false);
- MediaGraph.Nodes(g_SiteSounds).Index = index;
- MediaGraph.Nodes(g_SiteSounds).LoadMedia(false);
- }
- else
- {
- // alert( "DEBUG Nothing interesting around, skipping to next sound " + g_Range[g_Continent] );
- PlaySound(); // Nothing interesting around, go on to next sound
- }
- }
-
- function PlaySiteSoundNow()
- {
- // since the source of our sounds are specified with an URL, they
- // are downloaded asynchronously, and we need to wait until we are
- // notified that we can play it. Otherwise, script error.
- MediaGraph.Nodes("SiteChannel").Play();
- }
-
- // NOTE: This function is called from the Shell through Options dialog
- function SoundsOn()
- {
- if ( g_fFusionSrcAllLoaded )
- {
- g_fSoundsEnabled = true;
- ResumeSounds();
- } else if ( ((null == window.external.GetPackedSettings) ||
- (window.external.GetPackedSettings() & 0x00000004)) &&
- MediaGraph.OutputDeviceAvailable ) {
- g_fSoundsEnabled = true;
- LoadSounds();
- }
- }
-
- // NOTE: This function is called from the Shell through Options dialog
- function SoundsOff()
- {
- if ( g_fSoundsEnabled )
- {
- PauseSounds();
- }
- g_fSoundsEnabled = false;
- }
-
- function StopSounds()
- {
- // stop sounds and reset the play list
- MediaGraph.Nodes("WindChannel").Stop();
- MediaGraph.Nodes("SiteChannel").Stop();
- MediaGraph.Nodes("FusionChannel").Stop();
- g_PlayIndex = 0;
- }
-
- function PauseSounds()
- {
- if ( g_fSoundsActive )
- {
- // PlayState of 1 means that it is currently playing
- if( 1 == MediaGraph.Nodes("WindChannel").PlayState )
- {
- MediaGraph.Nodes("WindChannel").Pause();
- }
- if( 1 == MediaGraph.Nodes("SiteChannel").PlayState )
- {
- MediaGraph.Nodes("SiteChannel").Pause();
- }
- if( 1 == MediaGraph.Nodes("FusionChannel").PlayState )
- {
- MediaGraph.Nodes("FusionChannel").Pause();
- }
- }
- }
-
- function ResumeSounds()
- {
- if ( g_fSoundsEnabled && g_fSoundsActive )
- {
- // PlayState of 2 means that it is currently Paused
- if( 2 == MediaGraph.Nodes("WindChannel").PlayState )
- {
- MediaGraph.Nodes("WindChannel").Play();
- }
- if( 2 == MediaGraph.Nodes("SiteChannel").PlayState )
- {
- MediaGraph.Nodes("SiteChannel").Play();
- }
- if( 2 == MediaGraph.Nodes("FusionChannel").PlayState )
- {
- MediaGraph.Nodes("FusionChannel").Play();
- }
- }
- }
-
- function SetContinent( Continent )
- {
- for ( i=0; i<g_Continent_Num; i++ )
- {
- if ( Continent == g_Continent_Names[i] )
- {
- if ( g_fSoundsActive )
- {
- StopSounds();
- }
- g_Continent = i;
- g_SiteSounds = "SiteSounds" + g_Continent_Names[g_Continent];
- g_SiteSoundsNode = MediaGraph.Nodes(g_SiteSounds);
- if ( g_fSoundsActive )
- {
- PlaySound();
- }
- return;
- }
- }
- // alert( "DEBUG Attemping to set World Flight Continent to an undefined value " + g_Continent + ", " + Continent );
- }
-
- // Returns the index of the sound that is closest to the given lat, long.
- // If a (Lat/Long distance) Range is specified, a sound within that
- // range is randomly chosen. If no sound is within Range, a null
- // string is returned.
- // If Range is less than or equal to zero, the nearest sounds is returned
- function FindNearestSound( Lat, Long, Range )
- {
- if ( Range <= 0 )
- {
- return FindAnyNearestSound( Lat, Long );
- }
-
- var y, x, distance;
- var index = 0;
- var hits = new Array();
- var sum = 0.0;
-
- // maintain a list of all hits within range and randomly
- // select amoung those. hits are weighted relative to their
- // distance from the ship
- Range *= Range;
- for ( i=0; i < g_SoundTable[g_Continent].length; i++ )
- {
- y = Lat - g_SoundTable[g_Continent][i][g_indexLat];
- x = Long - g_SoundTable[g_Continent][i][g_indexLong];
- distance = (x*x) + (y*y);
- if ( distance < Range )
- {
- hits[index] = new Array(2);
- hits[index][0] = Range/distance;
- sum += hits[index][0];
- hits[index][1] = i;
- index++;
- }
- }
- var ran = Math.random();
-
- // While we test each hit, we increment the next hit's value
- // so hits[*][0]/sum covers the range from 0 to 1.
- for ( i=0; i < index; i++, hits[i][0]+=hits[i-1][0] )
- {
- if ( ran <= (hits[i][0]/sum) )
- {
- return g_SoundTable[g_Continent][hits[i][1]][g_indexSound];
- }
- }
- return 0;
- }
-
- // Returns the index of the sound that is closest to the given lat, long.
- function FindAnyNearestSound( Lat, Long )
- {
- var y = Lat - g_SoundTable[g_Continent][0][g_indexLat];
- var x = Long - g_SoundTable[g_Continent][0][g_indexLong];
- var distance = (x*x) + (y*y);
- var dist;
- var index = 0;
-
- for ( i=1; i < g_SoundTable[g_Continent].length; i++ )
- {
- y = Lat - g_SoundTable[g_Continent][i][g_indexLat];
- x = Long - g_SoundTable[g_Continent][i][g_indexLong];
- dist = (x*x) + (y*y);
- if ( dist < distance )
- {
- distance = dist;
- index = i;
- }
- }
- return g_SoundTable[g_Continent][index][g_indexSound];
- }
-
-